package com.example.springboot.controller;

import com.example.springboot.entity.ForumPostImage;
import com.example.springboot.service.ForumPostImageService;
import com.example.springboot.service.OSSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/images")
public class ForumPostImageController {

    @Autowired
    private ForumPostImageService forumPostImageService;

    @Autowired
    private OSSService ossService;

    /**
     * 上传图片并存储 URL 到 forum_post_images 表中
     */
    @PostMapping
    public void addImage(@RequestParam("file") MultipartFile file, @RequestParam("postId") Integer postId) throws Exception {
        String imageUrl = ossService.uploadImage(file); // 使用新方法上传图片
        ossService.saveImageUrlToDatabase(postId, imageUrl);
    }

    @DeleteMapping("/{imageId}")
    public void deleteImage(@PathVariable Integer imageId) {
        forumPostImageService.deleteImage(imageId);
    }

    @GetMapping("/post/{postId}")
    public List<ForumPostImage> getImagesByPost(@PathVariable Integer postId) {
        return forumPostImageService.getImagesByPostId(postId);
    }
}